package com.huang;

import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

/**
 * @author HuangShen
 * @Description
 * @create 2021-03-26 9:55
 */
public class Operation {

    @Test
    public void test1() throws Exception {

        Class<Student> studentClass = Student.class;
        Constructor<Student> constructor = studentClass.getConstructor(String.class, String.class);
        Student student = constructor.newInstance("HHH", "124356");

        Constructor<Student> declaredConstructor = studentClass.getDeclaredConstructor(String.class);
        declaredConstructor.setAccessible(true);
        Student student1 = declaredConstructor.newInstance("uuu");

        //获取类的属性
        Field[] declaredFields = studentClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName());
        }
        //获取到私有属性 id
        Field declaredField = studentClass.getDeclaredField(declaredFields[1].getName());
        declaredField.setAccessible(true);
        //给student1 的i私有属性id 设置1232
        declaredField.set(student1,"1232");
        //获取私有方法 getId
        Method getId = studentClass.getDeclaredMethod("getId");
        getId.setAccessible(true);
        //执行方法getId
        String invoke = (String) getId.invoke(student1);
        System.out.println(invoke);



    }


    @Test
    public void test2() throws Exception {
        Class<?> aClass = Class.forName("com.huang.Student");
        Class<?>[] interfaces = aClass.getInterfaces();
        Type genericSuperclass = aClass.getGenericSuperclass();
        System.out.println(genericSuperclass.getTypeName());

        Constructor<?> constructor = aClass.getDeclaredConstructor(String.class);
        constructor.setAccessible(true);


        MyAnnotation annotation = aClass.getAnnotation(MyAnnotation.class);


        Student o =(Student) constructor.newInstance(annotation.name());
        System.out.println(o.getName());

    }




}
